package com.etlmaster.util.vfs;

import java.io.IOException;
import java.util.Collection;

import org.apache.commons.vfs2.Capability;
import org.apache.commons.vfs2.FileName;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystem;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.provider.AbstractFileName;
import org.apache.commons.vfs2.provider.AbstractFileSystem;
import org.apache.commons.vfs2.provider.GenericFileName;


/**
 * @author Larry
 * @copyright Larry@etlMaster
 * @since 2017-09-10
 */
public class HdfsFileSystem extends AbstractFileSystem implements FileSystem {
	
	
	private org.apache.hadoop.fs.FileSystem fileSystem = null;

	protected HdfsFileSystem(FileName rootName, FileObject parentLayer,FileSystemOptions fileSystemOptions) {
		super(rootName, parentLayer, fileSystemOptions);
	}

	protected FileObject createFile(AbstractFileName name) throws Exception {
		return new HdfsFileObject(name, this);
	}

	public org.apache.hadoop.fs.FileSystem getHdfsFileSystem() {
		if(fileSystem==null){
			GenericFileName genericFileName = (GenericFileName) getRootName();
			fileSystem = HdfsUtils.getFileSystem(genericFileName.getHostName(), genericFileName.getPort());
		}
		return fileSystem;
	}

	protected void addCapabilities(Collection<Capability> caps) {
		caps.addAll(HdfsFileProvider.capabilities);
	}

	protected void putFileToCache(FileObject file) {
	}

	protected FileObject getFileFromCache(FileName name) {
		return null;
	}

	protected void doCloseCommunicationLink() {
		try {
			fileSystem.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}